﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraPivotGrid;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class Ordine : XForm
    {
        int CKBatchId = -1;
        public Ordine(int ckBatchId)
        {
            InitializeComponent();

            this.CKBatchId = ckBatchId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
        }

        private void Ordine_Load(object sender, EventArgs e)
        {
            
            //this.panelControl1.Visible = false;
            //bs = new BindingSource(ds, "xOrdine");
            RefreshData();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
            btnExport.ItemClick += btnExport_ItemClick;

            pvg.OptionsPrint.PrintColumnHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintDataHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.MergeRowFieldValues = false;
            pvg.OptionsPrint.MergeColumnFieldValues = false;
        }

        void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExportPivot(pvg);
        }

        BindingSource bs;
        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        void RefreshData()
        {
            bs = gridView1.Init("Get7cOrdine", CKBatchId);

            //DrillDownGrid(gridView1, bs);
            gridView1.AddSummary("QtaPz");
            gridView1.OptionsBehavior.Editable = false;

            InitPivot(pvg, bs);

            //GetDetails("xOrdine", "Get7cOrdine", CKBatchId);
            //gridView1.Columns["idx"].Visible = 
            //gridView1.Columns["CodCliente"].Visible = false;
            pvg.Cells.Selection = new Rectangle(new Point(0, 0), new Size(1, 1));
            pvg.Fields["Modello"].BestFit();
            pvg.BestFit();
        }

        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pvg, BindingSource bs)
        {
            pvg.BeginUpdate();
            pvg.DataSource = bs;

            if (!PivotInitialized)
            {
                pvg.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                pvg.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pvg.CellSelectionChanged += new EventHandler(pvg_CellSelectionChanged);
                pvg.FocusedCellChanged += new EventHandler(pvg_FocusedCellChanged);
                pvg.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pvg.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);

                AddNewField(pvg, "DosyaNo", PivotArea.FilterArea);
                AddNewField(pvg, "Linea", PivotArea.FilterArea);
                AddNewField(pvg, "Tex", PivotArea.FilterArea);

                PivotGridFieldBase[] pgf1 = new PivotGridFieldBase[3];
                pgf1[0] = AddNewField(pvg, "Linea", PivotArea.RowArea);
                pgf1[1] = AddNewField(pvg, "Modello", PivotArea.RowArea);
                pgf1[1].ExpandedInFieldsGroup = false;
                pgf1[2] = AddNewField(pvg, "Tex", PivotArea.RowArea);
                pvg.Groups.Add(pgf1, "ModelloTex");

                AddNewField(pvg, "Taglia", PivotArea.ColumnArea);

                AddNewField(pvg, "QtaPz", PivotArea.DataArea).Caption = "TotalQty";

                foreach (PivotGridField pgf in pvg.Fields)
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }
                PivotInitialized = true;
            }
            pvg.EndUpdate();
        }

        void pvg_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }


        void pvg_CellSelectionChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        private void CellChanged()
        {
            double PivotSum = 0.0;

            if (pvg.Cells.MultiSelection.SelectedCells.Count > 0)
            {
                foreach (Point cell in pvg.Cells.MultiSelection.SelectedCells)
                {
                    DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetCellInfo(cell.X, cell.Y);
                    if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                        cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                        PivotSum += Convert.ToDouble(pvg.GetCellValue(cell.X, cell.Y));
                }
            }
            else
            {
                DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetFocusedCellInfo();
                if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                    cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                    PivotSum += Convert.ToDouble(pvg.GetCellValue(pvg.Cells.FocusedCell.X, pvg.Cells.FocusedCell.Y));

            }
            //lblPivot.Text = PivotSum == 0 ? string.Empty : String.Format("{0:#,##0.## mt'.'}", PivotSum);
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl1.DataSource = e.CreateDrillDownDataSource();
            ExpandCollapse(splitContainerControl1, (gridView1.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView1.BestFitColumns();
            gridView1.MoveFirst();
        }
    }
}